package com.itany.hotel.service.impl;

import com.itany.hotel.constants.Constants;
import com.itany.hotel.dao.SubscriptionMapper;
import com.itany.hotel.entity.Subscription;
import com.itany.hotel.entity.SubscriptionDtl;
import com.itany.hotel.service.SubscriptionService;
import framework.bean.factory.ObjectFactory;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class SubscriptionServiceImpl implements SubscriptionService {
    @Override
    public List<Subscription> findUnacknowledged(Integer memberId) {

        SubscriptionMapper subscriptionDao = (SubscriptionMapper) ObjectFactory.getObject("subscriptionMapper");

        return subscriptionDao.selectByMidAndStatuses(memberId,new String[]{Constants.SubscriptionStatus.SUBSCRIPTION_STATUS_UNACKNOWLEDGED});

    }

    @Override
    public List<Subscription> findHistorical(Integer memberId) {

        SubscriptionMapper subscriptionDao = (SubscriptionMapper) ObjectFactory.getObject("subscriptionMapper");

        return subscriptionDao.selectByMidAndStatuses(memberId,new String[]{Constants.SubscriptionStatus.SUBSCRIPTION_STATUS_ACKNOWLEDGED,
                                                                            Constants.SubscriptionStatus.SUBSCRIPTION_STATUS_CANCELED});

    }

    @Override
    public void addDetail(List<SubscriptionDtl> subscriptionDtls) {

        for (SubscriptionDtl subscriptionDtl : subscriptionDtls) {
            // Double price=?????;
            // subscriptionDtl.setPrice(price);


            //调用持久层保存每一个明细，返回主键

            //根据开始日期和结束日期，每天往居住信息表中插入一条记录

            Date startDate=subscriptionDtl.getSdate();
            Date endDate=subscriptionDtl.getEdate();

            Calendar startCalendar=Calendar.getInstance();
            startCalendar.setTime(startDate);
            Calendar endCalendar=Calendar.getInstance();
            endCalendar.setTime(endDate);

            while(startCalendar.before(endCalendar)){

                //调用持久层保存一个居住信息


                startCalendar.add(Calendar.DAY_OF_MONTH,1);
            }



        }




    }
}
